coupon = {}

coupon.pageIndex = 0;
coupon.pageSize = 100;
coupon.rs;
coupon.list = function(){
    main.init();
    coupon.currentPage = 0;
    coupon.pageIndex = 0;
    coupon.pageSize = 100;
    coupon.startTime = 0;
    coupon.endTime = 0;
    coupon.id = 0;
    coupon.getData();
    $("#content").html(fly.template('http://naima.vn/coupon/main.html',{
        data: null
    }));                
    $('#sl-page-size li[num]').click(function(){
        coupon.pageIndex = 0;
        coupon.pageSize = $(this).attr('num');
        coupon.load();
    });
    coupon.load();
                
    $("#list-checkall").click(function(){
        $('input[type=checkbox][for]').attr('checked', $('#list-checkall').is(':checked'))
    });
}
coupon.all = function(){
    coupon.id = 0;
    coupon.load();
}
coupon.search = function(index){ 
    coupon.currentPage = 0;
    coupon.id = $("#txt-search-id").val(),
    coupon.load();
}

coupon.page = function(index){
    coupon.currentPage = index;
    coupon.load();
}

coupon.load = function(){
    fly.ajax({
        service: '/coupon/list',
        data: {
            id: coupon.id,
            pageIndex: coupon.pageIndex,
            pageSize: coupon.pageSize,
            currentPage: coupon.currentPage,
            startTime: coupon.startTime,
            endTime: coupon.endTime
        },
        success: function(result){
            if(result.status){
                //Hiển thị danh sách
                $("#tbl-list").html(fly.template('http://naima.vn/coupon/list.html',{
                    data:result.data.data
                }));
                
                //Hiển thị link phân trang
                $("#paging").html(fly.template('http://naima.vn/component/paging.html',{
                    data: result.data,
                    action: 'coupon'
                }));
                
                //Hiển thị tổng số bản ghi
                $("#lbl-datacount").text(result.data.length);
                
                //Link thay đổi số bản ghi trên trang
                $('#sl-page-size li[num]').removeClass('totalviewactive');
                $('#sl-page-size li[num='+result.data.pageSize+']').addClass('totalviewactive');
                $('#list-checkall').click(function(){
                    $('input[type=checkbox][for]').attr('checked', $('#list-checkall').is(':checked'));
                });                
                $('[title]').qtip({      
                    position: {
                        my: 'top center', /*'bottom left', 'bottom right', 'bottom center','top left', 'top right', 'top center', 'left center', 'left top', 'left bottom', 'right center', 'right top', 'right bottom', 'center'*/
                        at: 'center bottom' /*'top left', 'top right', 'top center','bottom left', 'bottom right', 'bottom center', 'right center', 'right top', 'right bottom', 'left center', 'left top', 'left bottom', 'center'*/
                    },
                    style: {
                        width:500,
                        classes: 'ui-tooltip-shadow ui-tooltip-bootstrap'
                    //Hỗ trợ: 'red', 'blue', 'dark', 'light', 'green', 'jtools', 'plain', 'youtube', 'cluetip', 'tipsy', 'tipped', 'bootstrap'
                    }                    
                })
                
            } else {
                
            }
        }
    });
    
}

coupon.create = function(){
    popup.open('popup-coupon-form', 'Thêm coupon mới', fly.template('http://naima.vn/coupon/form.html',{
        data: null
    }),[
    {
        title: 'Thêm mới',
        fn:function(){            
            $('.groupIds').val($('#groupId').val());
            $('.catIds').val($('.category').val());
            $('.brandIds').val($('.brand').val());
            $('.sellerIds').val($('.seller').val());
            var g = $('#groupId').val();
            if(g != null)
                if(g[0] == 0)
                    $('.groupIds').val(0);
            var c = $('.category').val();
            if(c != null)
                if(c[0] == 0)
                    $('.catIds').val(0);
            var b = $('.brand').val();
            if(b != null)
                if(b[0] == 0)
                    $('.brandIds').val(0);
            var s = $('.seller').val();                
            if(s != null)
                if(s[0] == 0)
                    $('.sellerIds').val(0);
            fly.submit({
                id: 'coupon-form',
                service: '/coupon/create',
                success: function(result){
                    popup.msg(result.message);
                    coupon.load();
                    popup.close('popup-coupon-form');
                }
            });
        }
    },
    {
        title: 'Hủy bỏ',
        fn:function(){
            popup.close('popup-coupon-form');
        }
    },
    ]);
    datepicker.init();
    coupon.printData();
    fly.editor('txt-content',{
        width:800
    });  
    popup.resetPos();
}

coupon.edit = function(id){
    fly.ajax({
        service: '/coupon/get/',
        data: {
            id: id
        },
        success: function(result){
            popup.open('popup-coupon-form', 'Sửa bài viết', fly.template('http://naima.vn/coupon/form.html',{
                data:result.data
            }),[
            {
                title: 'Sửa',
                fn:function(){
                    $('.groupIds').val($('#groupId').val());
                    $('.catIds').val($('.category').val());
                    $('.brandIds').val($('.brand').val());
                    $('.sellerIds').val($('.seller').val());
                    var g = $('#groupId').val();
                    if(g != null)
                        if(g[0] == 0)
                            $('.groupIds').val(0);
                    var c = $('.category').val();
                    if(c != null)
                        if(c[0] == 0)
                            $('.catIds').val(0);
                    var b = $('.brand').val();
                    if(b != null)
                        if(b[0] == 0)
                            $('.brandIds').val(0);
                    var s = $('.seller').val();                
                    if(s != null)
                        if(s[0] == 0)
                            $('.sellerIds').val(0);
                    fly.submit({
                        id: 'coupon-form',
                        service: '/coupon/edit',
                        success: function(rs){
                            popup.msg(rs.message);
                            coupon.load();
                            popup.close('popup-coupon-form');
                        }
                    });
                }
            },
            {
                title: 'Hủy bỏ',
                fn:function(){
                    popup.close('popup-coupon-form');
                }
            }
            ]);            
            datepicker.init();
            coupon.printData();
            $('#groupId').val(result.data.groupId.split(','));
            $('.brand').val(result.data.brandId.split(','));
            $('.category').val(result.data.categoryId.split(','));
            $('.seller').val(result.data.sellerId.split(','));
            fly.editor('txt-content',{
                width:800
            });  
            popup.resetPos();
        }
        
    });
     
}

coupon.del = function(id){
    popup.confirm("Bạn có chắc muốn xóa bài viết này ?", function(){
        fly.ajax({
            service: '/coupon/delete',
            data: {
                id:id
            },
            success: function(result){
                popup.msg(result.message);
                $(".table tr[for="+result.data+"]").remove();
            }
        });
    });
    
}
coupon.changeStatus = function(id){
    fly.ajax({
        service: '/coupon/changeStatus/',
        data: {
            id: id
        },
        success: function(data){
            if(data.status){
                if(data.data == 1)
                    $('img[auto='+id+']').attr('src',fly.assetsUrl + '/images/icon-enable.png');
                else
                    $('img[auto='+id+']').attr('src',fly.assetsUrl + '/images/icon-disable.png');
            }
            else
                popup.msg(data.message);
        }
    });
}

coupon.getData = function(){
    if(!coupon.group){
        fly.ajax({
            service: '/group/list',
            async: false,
            loading: false,
            success: function(data){
                coupon.group = data.data;
            }
        })
    }
    if(!coupon.brand){
        fly.ajax({
            service: '/brand/list',
            async: false,
            loading: false,
            success: function(data){
                coupon.brand = data.data;
            }
        })
    }
    if(!coupon.brand){
        fly.ajax({
            service: '/brand/list',
            async: false,
            loading: false,
            success: function(data){
                coupon.brand = data.data;
            }
        })
    }
    if(!coupon.cat){
        fly.ajax({
            service: '/category/list',
            async: false,
            loading: false,
            success: function(data){
                coupon.cat = data.data;
            }
        })
    }
    if(!coupon.seller){
        fly.ajax({
            service: '/seller/list',
            async: false,
            loading: false,
            success: function(data){
                coupon.seller = data.data;
            }
        })
    } 
}
coupon.printData = function(){
    coupon.getData();
    $('#groupId, .brand, .category, .seller').html('<option value="0">Tất cả</option>');
    $.each(coupon.brand, function(){
        $('.brand').append('<option value="'+this.id+'">'+this.name+'</option>');
    })
    $.each(coupon.group, function(){
        $('#groupId').append('<option value="'+this.id+'">'+this.name+'</option>');
    })
    $.each(coupon.seller, function(){
        $('.seller').append('<option value="'+this.id+'">'+this.name+'</option>');
    })
    coupon.printCat(0);
}
coupon.level = 0;
coupon.printCat = function(parentId){
    $.each(coupon.cat, function(){
        var cat = this;
        if(cat.parentId == parentId){
            coupon.level ++;
            var tmp = "";
            var i = coupon.level;
            while(i){
                tmp += "- - ";
                i--;
            }
            $('.category').append('<option value="'+cat.id+'">'+tmp+cat.name+'</option>');
            coupon.printCat(cat.id);
            coupon.level --;
        }
    });
}
coupon.getName = function(type, id){
    var name = 'Tất cả';
    switch(type){
        case "group":
            $.each(coupon.group, function(){
                if(this.id == id)
                    name = this.name;
            })
            break;
        case "brand":
            $.each(coupon.brand, function(){
                if(this.id == id)
                    name = this.name;
            })
            break;
        case "cat":
            $.each(coupon.cat, function(){
                if(this.id == id)
                    name = this.name;
            })
            break;
        case "seller":
            $.each(coupon.seller, function(){
                if(this.id == id)
                    name = this.name;
            })
            break;
    }
    return name;
}